﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EventBinder_When_From_AddRemoveHandler.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the EventBinder_When_From_AddRemoveHandler type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Photon.Threading.EventPattern;

    [TestClass]
   public class EventBinder_When_From_AddRemoveHandler : EventBinder_When_Base
   {
      [TestMethod]
      public void If_Handler_Has_Been_Added_Should_Start_Receiving_Events()
      {
         var firedCount = 0;
         var source = new TestEventSource();
         var binder = EventBinder<EventArgs>.From(source.GetType(), "Fired");
         binder.AddHandler(source, (s, e) => firedCount++);
         
         Assert.AreEqual(0, firedCount);
         source.Fire();
         Assert.AreEqual(1, firedCount);
      }

      [TestMethod]
      public void If_Handler_Has_Been_Removed_Should_Stop_Receiving_Events()
      {
         var firedCount = 0;
         var source = new TestEventSource();
         var binder = EventBinder<EventArgs>.From(source.GetType(), "Fired");
         EventHandler<EventArgs> handler = (s, e) => firedCount++;

         // add handler and verify we recieve events
         binder.AddHandler(source, handler);
         Assert.AreEqual(0, firedCount);
         source.Fire();
         Assert.AreEqual(1, firedCount);

         // remove handler and verify we no longer recieve events
         binder.RemoveHandler(source, handler);
         source.Fire();
         Assert.AreEqual(1, firedCount);
      }
   }
}
